package Controllers;

import Entity.Software;
import Service.ISoftwareService;
import Util.Constant;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class SoftwareController {


    @Autowired
    private ISoftwareService iSoftwareService;

    @RequestMapping(value = "/api/SelectAllFolder", produces = "text/plain;charset=utf-8")
    public String selectAllFolder() {
        List<String> strings = iSoftwareService.selectAllFolder();
        Gson gson = new GsonBuilder().create();
        String json = gson.toJson(strings);
        System.out.println("SelectAllFolder:" + json.toString());
        return json;
    }


    @RequestMapping(value = "/api/SelectFileInFolder", method = RequestMethod.POST, consumes = "application/json", produces = "text/plain;charset=utf-8")
    public String selectFileInFolder(@RequestBody String filefolder) {
        System.out.println("SelectFileInFolder1111111111111111111:" + filefolder);
        JSONObject jo = new JSONObject();
        Map<String, Object> m = (Map<String, Object>) jo.parse(filefolder);
        String folder = "";
        if (m != null) {
            folder = m.get("filefolder").toString();
        }
        List<Software> softwares = iSoftwareService.selectFileInFolder(folder);
        for (Software software : softwares) {
            software.setLocation(Constant.SOFTWARE_HEAD_ADDRESS + "/" + software.getFilefolder() + "/" + software.getName());
        }
        Gson gson = new GsonBuilder().create();
        String json = gson.toJson(softwares);
        System.out.println("JSON:" + json.toString());
        return json;
    }

}
